package com.david.mydemo.framework.aspect;


import com.david.mydemo.framework.annotation.MySysLog;
import com.david.mydemo.ctl_ftp.service.ISysLogService;
import com.david.mydemo.util.LoginUserDto;
import com.david.mydemo.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 系统操作日志切面配置
 */
@Aspect
@Component
@Slf4j
public class MySysLogAspect {

    @Autowired
    private ISysLogService sysLogService;

    @Pointcut("@annotation(com.david.mydemo.framework.annotation.MySysLog)")
    public void sysLogPointcut() {

    }

    @Before("sysLogPointcut()")
    public void beforePointcut(JoinPoint joinPoint) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        MySysLog annotation = methodSignature.getMethod().getAnnotation(MySysLog.class);
        String[] params = methodSignature.getParameterNames(); // 获取参数名称

        // 获取token中的user操作人
        LoginUserDto loginUserDto = TokenUtil.getUser(request);
        String user = loginUserDto != null ? loginUserDto.getUserId() : "anonymous-user";
        sysLogService.saveSysLog(user, annotation.operate(), annotation.module(), annotation.content());

        // 保存日志数据
        log.info("==============保存日志数据成功，请求参数名：{}，参数值：{}", params, joinPoint.getArgs());

    }

//    @Around("sysLogPointcut()")
//    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
////        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
////        String[] params = methodSignature.getParameterNames(); // 获取参数名称
////        Object[] args = joinPoint.getArgs(); // 获取参数值
////        Class[] paramTypes = methodSignature.getParameterTypes(); // 获取参数类型
//
//        return joinPoint.proceed();
//    }

//    @After("sysLogPointcut()")
//    public Object after(ProceedingJoinPoint joinPoint) throws Throwable {
//        return joinPoint.proceed();
//    }

//    @AfterReturning(value = "sysLogPointcut()", returning = "result")
//    public void doAfterReturning(JoinPoint joinPoint, Object result) {
////        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
////        MySysLog annotation = methodSignature.getMethod().getAnnotation(MySysLog.class);
////        String[] params = methodSignature.getParameterNames(); // 获取参数名称
////        Object[] args = joinPoint.getArgs(); // 获取参数值
//    }


}

